<script lang="ts" setup>
import RightTitle from './RigthTitle.vue'
import {baroption} from '../../utils/option'
import {ref} from 'vue'
import {staffTurnover} from '../../api/index'
import ChartsView from '../../components/ChartsView.vue'
import * as echarts from 'echarts'
import useResize from '../../hook/useResize'
useResize()
const key = ref(0)
// 定义图表渐变的颜色数组
let colorArr = [
    [
        {
            offset: 0,
            color: '#2339ff'
        },
        {
            offset: 0.5,
            color: '#731cff'
        },
        {
            offset: 1,
            color: '#ba02ff'
        }
    ],
    [
        {
            offset: 0,
            color: '#3ee293'
        },
        {
            offset: 0.5,
            color: '#21bf52'
        },
        {
            offset: 1,
            color: '#049c11'
        }
    ]
]
interface typeArr{
    [key:string]: any
}
// 发请求获取数据
staffTurnover().then(resp => {
    // console.log(111222, resp);
    let data = resp.data.data.result[0]
    console.log(2224444444444, data);
    /*
     xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    series: [
        {
        data: [120, 200, 150, 80, 70, 110, 130],
        type: 'bar'
        }
    ]
    */
    let arr:Array<typeArr> = []
    data.dimensions.forEach((v: string) => {
        arr.push(data.source.map((item: typeArr) => item[v]))
    })
    baroption.xAxis = {
        type: 'category',
        data: arr.shift()
    }
    baroption.series = arr.map((v, index) => {
        return {
            type: 'bar',
            data: v,
            name: data.dimensions[index + 1],
            itemStyle: {
                // 圆角
                borderRadius: [20, 20, 20, 20],
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, colorArr[index])
            }
        }
    })
   
    // console.log(333, barOption);
    key.value = +new Date
})

</script>

<template>
    <div class="right-cotainer">
        <!-- 上面的标题 -->
        <Right-Title title="校园人员流动情况"></Right-Title>
        <!-- 下面部分 -->
        <div class="echarts">
            <!-- 上面头像部分 -->
            <div class="avatar">
                <div>
                    <img src="../../assets/昨日.png" alt="">
                    <div class="wrap">
                        <span>1</span>
                        <p>昨日外来访客</p>
                    </div>
                </div>
                <div>
                    <img src="../../assets/今日.png" alt="">
                    <div class="wrap">
                        <span>1</span>
                        <p>今日外来访客</p>
                    </div>
                </div>
            </div>
            <!-- 图表 -->
            <div class="rb-charts">
                <ChartsView ref="mycharts" :option="baroption" :key="key"></ChartsView>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
.right-cotainer{
    width: 100%;
    height: 45%;
    position: absolute;
    right: 0;
    bottom: 0;
    padding: 0 50px 0 70px;
    box-sizing: border-box;
    .echarts{
        width: 100%;
        height: 70%;
        background-color: rgba(120,120,120,.1);
        color: #ccc;
        .avatar{
            width: 100%;
            height: 50px;
            display: flex;
            justify-content: space-around;
            align-items: center;
            >div{
                display: flex;
                justify-content: space-between;
                align-items: center;
                .wrap{
                    text-align: center;
                    font-size: 12px;
                    margin-left: 20px;
                    p{
                        padding: 0;
                        margin: 0;
                    }
                }
                img{
                    width: 20px;
                    height: 20px;
                }
            }
        }
        .rb-charts{
            width: 100%;
            height: 100%;
        }
    }
}
</style>